import Mock from 'mockjs';
const BASE_URL = 'http://localhost:8080';

// Mock 教师登录接口
Mock.mock(`${BASE_URL}/teacher/login`, 'post', (options) => {
    const { tch_id, tch_password } = JSON.parse(options.body);

    if (tch_id === '123' && tch_password === 'aaaaaa') {
        return {
            code: 200,
            message: '登录成功',
            user: { username: tch_id }
        };
    } else {
        return {
            code: 401,
            message: '用户名或密码错误'
        };
    }
});

// Mock 学生登录接口
Mock.mock(`${BASE_URL}/student/login`, 'post', (options) => {
    const { stu_id, stu_password } = JSON.parse(options.body); // 修改为 stu_id 和 stu_password

    if (stu_id === '111' && stu_password === 'aaaaaa') {
        return {
            code: 200,
            message: '登录成功',
            user: { username: stu_id }
        };
    } else {
        return {
            code: 401,
            message: '用户名或密码错误'
        };
    }
});



// Mock 课程内容接口
Mock.mock(`${BASE_URL}/course/description`, 'post', {
    data: '这是课程介绍的详细内容。',
});

Mock.mock(`${BASE_URL}/course/syllabus`, 'post', {
    data: '这是教学大纲的详细内容。',
});

Mock.mock(`${BASE_URL}/course/calendar`, 'post', {
    data: '这是教学日历的详细内容。',
});

Mock.mock(`${BASE_URL}/course/homework`, 'post', {
    data: '这是作业的详细内容。',
});

Mock.mock(`${BASE_URL}/course/exam`, 'post', {
    data: '这是结课考核的详细内容。',
});

// Mock 文件列表接口
Mock.mock(`${BASE_URL}/api/files`, 'post', {
    data: [
        { id: 1, name: '课件1.pdf', url: `${BASE_URL}/files/课件1.pdf` },
        { id: 2, name: '课件2.pdf', url: `${BASE_URL}/files/课件2.pdf` },
        { id: 3, name: '课件3.pdf', url: `${BASE_URL}/files/课件3.pdf` },
    ],
});

// Mock 文件上传接口
Mock.mock(`${BASE_URL}/api/upload`, 'post', {
    success: true,
    message: '文件上传成功',
});

// Mock 更新内容接口
Mock.mock(`${BASE_URL}/api/update`, 'post', {
    success: true,
    message: '内容保存成功',
});


// Mock 获取用户信息接口
Mock.mock(`${BASE_URL}/student/showStudentMsg`, 'post', {
    data: {
        stu_name: '张三',
        stu_class: '软件工程 2022 级',
        stu_email: 'zhangsan@example.com',
    },
});

// Mock 获取课程列表接口
Mock.mock(`${BASE_URL}/student/stuCourse`, 'post', {
    data: [
        { course_id: 101, course_name: '数据结构' },
        { course_id: 102, course_name: '操作系统' },
        { course_id: 103, course_name: '计算机网络' },
    ],
});

// Mock 修改密码接口
Mock.mock(`${BASE_URL}/student/update`, 'put', {
    success: true,
    message: '密码修改成功',
});


// Mock 课程内容接口
Mock.mock(`${BASE_URL}/course/description`, 'post', {
    data: '这是课程介绍的详细内容。',
});

Mock.mock(`${BASE_URL}/course/syllabus`, 'post', {
    data: '这是教学大纲的详细内容。',
});

Mock.mock(`${BASE_URL}/course/calendar`, 'post', {
    data: '这是教学日历的详细内容。',
});

Mock.mock(`${BASE_URL}/course/homework`, 'post', {
    data: '这是作业的详细内容。',
});

Mock.mock(`${BASE_URL}/course/exam`, 'post', {
    data: '这是结课考核的详细内容。',
});

// Mock 文件列表接口
Mock.mock(`${BASE_URL}/api/files`, 'post', {
    data: [
        { id: 1, name: '课件1.pdf', url: `${BASE_URL}/files/课件1.pdf` },
        { id: 2, name: '课件2.pdf', url: `${BASE_URL}/files/课件2.pdf` },
        { id: 3, name: '课件3.pdf', url: `${BASE_URL}/files/课件3.pdf` },
    ],
});

// Mock 文件上传接口
Mock.mock(`${BASE_URL}/api/upload`, 'post', {
    success: true,
    message: '文件上传成功',
});

// Mock 更新内容接口
Mock.mock(`${BASE_URL}/course/updatedescription`, 'put', (options) => {
    const { course_description } = JSON.parse(options.body);
    return {
        success: true,
        message: `课程介绍更新成功：${course_description}`,
    };
});

Mock.mock(`${BASE_URL}/course/updatesyllabus`, 'put', (options) => {
    const { syllabus } = JSON.parse(options.body);
    return {
        success: true,
        message: `教学大纲更新成功：${syllabus}`,
    };
});

Mock.mock(`${BASE_URL}/course/updatecalendar`, 'put', (options) => {
    const { calendar } = JSON.parse(options.body);
    return {
        success: true,
        message: `教学日历更新成功：${calendar}`,
    };
});

// Mock 获取教师信息接口
Mock.mock(`${BASE_URL}/teacher/showTeacherMsg`, 'post', {
    data: {
        tch_name: '李老师',
        tch_title: '副教授',
        tch_department: '软件学院',
        tch_email: 'teacher@example.com',
    },
});

// Mock 获取课程列表接口
Mock.mock(`${BASE_URL}/teacher/tchCourse`, 'post', {
    data: [
        { course_id: 201, course_name: '数据结构' },
        { course_id: 202, course_name: '计算机网络' },
        { course_id: 203, course_name: '操作系统' },
    ],
});

// Mock 修改密码接口
Mock.mock(`${BASE_URL}/teacher/update`, 'put', (options) => {
    const { tch_password } = JSON.parse(options.body);
    if (tch_password.length >= 6) {
        return {
            success: true,
            message: '密码修改成功！',
        };
    } else {
        return {
            success: false,
            message: '密码至少6位字符',
        };
    }
});



// 模拟帖子数据
const posts = [
    { id: 1, title: '如何学习Vue.js？', content: '这是关于Vue.js学习的帖子内容。', author: '用户A', date: '2024-11-01', follows: 10, replies: 5 },
    { id: 2, title: 'Vue Router 使用指南', content: 'Vue Router 是 Vue.js 官方的路由管理库。', author: '用户B', date: '2024-11-02', follows: 8, replies: 3 },
    { id: 3, title: '如何优化前端性能', content: '在前端开发中优化性能是非常重要的。', author: '用户C', date: '2024-11-03', follows: 15, replies: 6 },
    { id: 4, title: '前端开发框架对比', content: 'Vue、React 和 Angular 是最常见的前端框架。', author: '用户D', date: '2024-11-04', follows: 20, replies: 12 },
    { id: 5, title: '如何使用 Mock.js', content: 'Mock.js 是一个用于模拟数据的库。', author: '用户E', date: '2024-11-05', follows: 7, replies: 2 },
];

// Mock 获取帖子列表接口
Mock.mock(`${BASE_URL}/api/post`, 'post', {
    code: 200,
    data: posts
});

// Mock 发布新主题接口
Mock.mock(`${BASE_URL}/api/submitPost`, 'post', (options) => {
    const { title, content, images } = JSON.parse(options.body);
    return {
        code: 200,
        message: '发帖成功',
        data: {
            id: Mock.Random.guid(),
            title: title,
            content: content,
            author: '当前用户',
            date: Mock.Random.date(),
            views: 0,
            replies: 0,
            votes: 0,
            images: images
        }
    };
});

// Mock 图片上传接口
Mock.mock(`${BASE_URL}/api/uploadImage`, 'post', {
    code: 200,
    message: '图片上传成功',
    url: `${BASE_URL}/uploads/image-${Mock.Random.guid()}.jpg`
});

// 模拟帖子搜索接口
Mock.mock(`${BASE_URL}/api/searchPosts`, 'post', (options) => {
    const { keyword } = JSON.parse(options.body);

    // 过滤包含关键词的帖子
    const filteredPosts = posts.filter(post =>
        post.title.includes(keyword) || post.content.includes(keyword)
    );

    return {
        code: 200,
        data: filteredPosts, // 返回符合条件的帖子列表
        message: filteredPosts.length ? '搜索成功' : '未找到相关帖子'
    };
});


// Mock 获取帖子详情接口
Mock.mock(`${BASE_URL}/api/postDetail`, 'post', (options) => {
    const { postId } = JSON.parse(options.body);
    return {
        code: 200,
        post: {
            id: postId,
            title: '如何提高学习效率？',
            date: '2024-11-06',
            content: '请大家分享一些提升学习效率的好方法！'
        },
        replies: [
            { id: 1, author: '张三', date: '2024-11-06', content: '我觉得制定计划很重要。', upvotes: 3, liked: false },
            { id: 2, author: '李四', date: '2024-11-06', content: '可以试试番茄工作法！', upvotes: 5, liked: false }
        ]
    };
});

// Mock 提交回复接口
Mock.mock(`${BASE_URL}/api/submitReply`, 'post', (options) => {
    const { content, userId } = JSON.parse(options.body);
    return {
        code: 200,
        message: '回复成功',
        reply: {
            id: Mock.Random.guid(),
            author: userId,
            date: Mock.Random.date(),
            content: content,
            upvotes: 0,
            liked: false
        }
    };
});



// 模拟检查用户是否已点赞回复
Mock.mock(`${BASE_URL}/api/checkLikes`, 'post', (options) => {
    const { userId, postId } = JSON.parse(options.body);
    return {
        code: 200,
        likedReplies: [1], // 假设用户对 ID 为 1 的回复已点赞
    };
});

// 模拟点赞接口
Mock.mock(`${BASE_URL}/api/likeReply`, 'post', (options) => {
    const { replyId, userId } = JSON.parse(options.body);
    return {
        code: 200,
        message: `用户${userId}点赞了回复${replyId}`,
    };
});

// 模拟取消点赞接口
Mock.mock(`${BASE_URL}/api/unlikeReply`, 'post', (options) => {
    const { replyId, userId } = JSON.parse(options.body);
    return {
        code: 200,
    };
});



// Mock 删除帖子接口
Mock.mock(`${BASE_URL}/api/deletePost`, 'post', (options) => {
    const { postId } = JSON.parse(options.body);
    return {
        code: 200,
        message: `帖子 ${postId} 已删除`
    };
});

// 发布通知
Mock.mock('http://localhost:8000/api/notices', 'post', (options) => {
    const notice = JSON.parse(options.body);  // 获取请求体中的通知数据

    if (!notice.title || !notice.content) {
        return {
            status: 400,
            message: '标题和内容不能为空'
        };
    }

    // 这里可以保存通知数据，模拟数据库保存
    // 例如，假设你有一个全局变量来保存通知
    const noticesList = []; // 假设你在实际代码中有这个数组
    notice.id = noticesList.length + 1; // 自动生成 ID
    noticesList.push(notice);

    return {
        status: 200,
        message: '通知发布成功',
        data: notice  // 返回发布的通知数据
    };
});

// 获取用户通知接口
const notificationsList = [
    { id: 1, title: '学校通知1', content: '学校将于11月举行期中考试，请同学们提前准备。', date: '2024-10-28', sender: '学校', type: 'school' },
    { id: 2, title: '课程通知1', content: '下周的数学课将调整为周五，请注意查看课表。', date: '2024-10-29', sender: '老师', type: 'course' },
    { id: 3, title: '学校通知2', content: '关于新学期的课程安排，详见官网。', date: '2024-10-30', sender: '学校', type: 'school' },
];
Mock.mock('http://localhost:8000/api/users/1/notifications', 'get', () => {

    return {
        status: 200,
        message: '获取通知成功',
        data: {
            unreadSchool: 1,  // 未读的学校通知数量
            unreadCourse: 2,  // 未读的课程通知数量
            notificationsList  // 通知列表
        },
    };
});

const teacherAssignments = [
    {
        id: 1,
        title: "数学作业",
        content: "完成课本第25页的练习题。",
        dueDate: "2024-11-05T13:00:00"
    },
    {
        id: 2,
        title: "英语作文",
        content: "写一篇关于你最喜欢的电影的作文，字数在300字左右。",
        dueDate: "2024-11-10T12:00:00"
    }
];
// const assignments = teacherAssignments;

// 教师端作业
Mock.mock('http://localhost:8000/api/teacherAssignments', 'get', {
    status: 200,
    message: '获取作业列表成功',
    data: teacherAssignments,  // 返回作业列表数据
});

// 模拟获取单个作业详情
Mock.mock(/http:\/\/localhost:8000\/api\/assignments\/\d+/, 'get', (options) => {
    const id = parseInt(options.url.split('/').pop());
    const assignment = assignments.find(a => a.id === id);

    if (assignment) {
        return {
            status: 200,
            message: '获取作业详情成功',
            data: assignment,
        };
    } else {
        return {
            status: 404,
            message: '作业未找到',
        };
    }
});

const assignmentDetail = {
    id: 1,
    title: '数学作业',
    dueDate: '2024-11-01',
    content: '请完成所有习题。',
};

const studentAssignment = [
    {
        id: 1,
        studentName: "张三",
        assignmentName: "数学作业",
        submissionTime: "2024-11-01T10:00:00",
        score: 0, // 默认分数
        comment: "111" // 默认评语
    },
    {
        id: 2,
        studentName: "李四",
        assignmentName: "英语作文",
        submissionTime: "2024-11-02T09:00:00",
        score: 0,
        comment: ""
    }
];

// 模拟获取学生提交的作业
Mock.mock('http://localhost:8000/api/studentAssignment', 'get', {
    status: 200,
    message: '获取学生作业列表成功',
    data: studentAssignment // 返回学生提交的作业数据
});

// Mock 用户信息接口
Mock.mock(`${BASE_URL}/api/userProfile/1`, 'get', {
    code: 200,
    data: {
        id: 1,
        username: '张三',
        avatarUrl: 'https://example.com/avatar.png',
        bio: '清北交复占一半。'
    }
});

// Mock 收藏夹列表接口
Mock.mock(`${BASE_URL}/api/favorites/1`, 'get', {
    code: 200,
    data: [
        {
            id: 1,
            title: "C++学习",
            posts: [
                { id: 101, title: "指针学习", description: "理解指针的使用。" },
                { id: 102, title: "数据结构学习", description: "深入了解数据结构。" }
            ]
        },
        {
            id: 2,
            title: "前端开发",
            posts: [
                { id: 103, title: "Vue教程", description: "Vue.js学习资源汇总。" },
                { id: 104, title: "React入门", description: "React的基本介绍。" }
            ]
        }
    ]
});

const assignments = [
    {
        homework_id: 1,
        due_date: "2024-11-05T13:00:00", // 确保此字段存在
        title: "数据结构",
        content: "完成计算器"
    },
    {
        homework_id: 2,
        due_date: "2024-11-10T12:00:00",
        title: "算法分析与设计",
        content: "完成动态规划"
    }
];
Mock.mock(`${BASE_URL}/homework/gethomework`, 'post', {
    code: 200,
    data: assignments
});

// Mock 特定收藏夹帖子列表接口
Mock.mock(new RegExp(`${BASE_URL}/api/favorites/\\d+/posts`), 'get', (options) => {
    const folderId = parseInt(options.url.match(/\/favorites\/(\d+)\/posts/)[1], 10);
    const favorites = [
        {
            id: 1,
            posts: [
                { id: 101, title: "指针学习", description: "理解指针的使用。" },
                { id: 102, title: "数据结构学习", description: "深入了解数据结构。" }
            ]
        },
        {
            id: 2,
            posts: [
                { id: 103, title: "Vue教程", description: "Vue.js学习资源汇总。" },
                { id: 104, title: "React入门", description: "React的基本介绍。" }
            ]
        }
    ];
    const folder = favorites.find(fav => fav.id === folderId);
    return {
        code: 200,
        data: folder ? folder.posts : []
    };
});

//考试接口
Mock.mock(`${BASE_URL}/api/setExam`, 'post', (options) => {
    const examData = JSON.parse(options.body);
    console.log("收到的考试设置信息：", examData);

    // 检查是否提供了必填项
    if (!examData.date || !examData.time || !examData.room) {
        return {
            success: false,
            message: '考试日期、时间和教室为必填项'
        };
    }

    return {
        success: true,
        message: '考试设置成功'
    };
});

// 教室数据
const rooms = [
    { id: 1, name: '101教室', capacity: 50 },
    { id: 2, name: '102教室', capacity: 30 },
    { id: 3, name: '201教室', capacity: 100 },
    { id: 4, name: '202教室', capacity: 40 },
    { id: 5, name: '301教室', capacity: 80 },
    { id: 6, name: '302教室', capacity: 25 }
];

// Mock 获取教室列表接口
Mock.mock(`${BASE_URL}/api/rooms`, 'get', {
    code: 200,
    data: rooms,
    message: '获取教室信息成功'
});

// 文件上传接口 (PDF试卷上传)
Mock.mock(`${BASE_URL}/api/upload`, 'post', () => {
    return {
        success: true,
        message: '试卷上传成功',
        url: `${BASE_URL}/uploads/exam-${Mock.Random.guid()}.pdf`
    };
});

// 模拟考试数据
const examAssignments = [
    {
        id: 1,
        dueDate: '2024-12-01',
        time: '14:00-16:00',
        room: 'yf106',
        seat: '顺序',
    },
    {
        id: 2,
        dueDate: '2024-12-02',
        time: '12:00-14:00',
        room: 'yf206',
        seat: '逆序',
    },
];

// Mock 获取考试数据接口
Mock.mock(`${BASE_URL}/api/examAssignments`, 'get', {
    status: 200,
    message: '获取考试列表成功',
    data: examAssignments,
});



// 模拟提交评分接口
Mock.mock(`${BASE_URL}/api/submitReviews`, 'post', (options) => {
    const body = JSON.parse(options.body);

    // 校验数据格式
    if (
        Array.isArray(body) &&
        body.every((item) => typeof item.id === 'string' && typeof item.score === 'number')
    ) {
        console.log('提交的评分数据:', body); // 打印评分数据，便于调试
        return {
            code: 200,
            message: '评分提交成功',
        };
    } else {
        return {
            code: 400,
            message: '提交数据格式错误，请检查！',
        };
    }
});


// 模拟作业提交
Mock.mock(`${BASE_URL}/assignment/submission`, 'post', (options) => {
    const formData = new Map();

    // 模拟 FormData 的解析
    options.body.forEach((value, key) => {
        formData.set(key, value);
    });

    const homework_id = formData.get('homework_id');
    const content = formData.get('content');
    const file = formData.get('file'); // 模拟文件字段

    if (!homework_id) {
        return {
            code: 400,
            message: '作业ID不能为空',
        };
    }

    return {
        code: 200,
        message: '作业提交成功',
        data: {
            homework_id,
            content: content || '无文本内容',
            file: file ? '附件已成功上传' : '无附件',
        },
    };
});

// 模拟互评作业数据
const revAssignments = [
    {
        title: '作业 1',
        fileUrl: 'https://example.com/homework1.pdf',
    },
    {
        title: '作业 2',
        fileUrl: 'https://example.com/homework2.pdf',
    },
    {
        title: '作业 3',
        fileUrl: 'https://example.com/homework3.pdf',
    },
];

// Mock 获取作业列表接口
Mock.mock(`${BASE_URL}/api/revAssignments`, 'post', {
    code: 200,
    message: '获取作业列表成功',
    data: revAssignments,
});

// Mock AI对话接口
Mock.mock(`${BASE_URL}/ai/ask`, 'post', (options) => {
    const { userMessage } = JSON.parse(options.body);

    let aiResponse = '';
    if (userMessage.includes('你好')) {
        aiResponse = '你好！很高兴和你聊天！';
    } else if (userMessage.includes('你是谁')) {
        aiResponse = '我是一个AI助手，专门为你提供帮助！';
    } else if (userMessage.includes('天气')) {
        aiResponse = '今天的天气很好，适合外出活动！';
    } else {
        aiResponse = '我没有理解你的问题，能否再试一次？';
    }

    return {
        code: 200,
        message: '请求成功',
        data: {
            aiResponse: aiResponse,
        },
    };
});
